package com.xuelang.pipeline.artifact.app.resource.impl;

import cn.hutool.core.util.StrUtil;
import com.xuelang.pipeline.artifact.app.harborv2.RepositoryService;
import com.xuelang.pipeline.artifact.app.nexus3v1.RepositoryFolderService;
import com.xuelang.pipeline.artifact.app.nexus3v1.request.DeleteRepositoryFolderRequest;
import com.xuelang.pipeline.artifact.app.nexus3v1.response.DeleteRepositoryFolderResponse;
import com.xuelang.pipeline.artifact.app.nexus3v1.xo.ApiResponseXO;
import com.xuelang.pipeline.artifact.app.resource.InstanceComponentService;
import com.xuelang.pipeline.artifact.client.common.constant.Constant;
import com.xuelang.pipeline.artifact.client.common.exception.BizException;
import com.xuelang.pipeline.artifact.client.resource.enums.ArtifactToolEnum;
import com.xuelang.pipeline.artifact.domain.resource.RepositoryComponent;
import com.xuelang.pipeline.artifact.domain.resource.RepositoryPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author xiwen.zxg
 * @since 2024/7/25
 */
@Service
public class InstanceComponentServiceImpl implements InstanceComponentService {

    /**
     * Nexus3仓库
     */
    @Autowired
    RepositoryFolderService repositoryFolderService;
    /**
     * Harbor仓库
     */
    @Autowired
    RepositoryService repositoryService;

    private String getInstanceRepositoryName(String repositoryName) {
        if (null == repositoryName) {
            return repositoryName;
        }
        if (!repositoryName.startsWith("hosted-")) {
            return repositoryName;
        }
        return repositoryName.replaceFirst("hosted-", "");
    }
    private String getHostedInstanceRepositoryName(String repositoryName) {
        if (null == repositoryName) {
            return repositoryName;
        }
        if (repositoryName.startsWith("hosted-")) {
            return repositoryName;
        }
        return "hosted-" + repositoryName;
    }

    @Override
    public void deleteComponentOfRepository(RepositoryPackage repositoryPackage, List<RepositoryComponent> repositoryComponentList) {

        ArtifactToolEnum.ArtifactTypeEnum artifactType = ArtifactToolEnum.ArtifactTypeEnum.getByKey(repositoryPackage.getArtifactType());
        String instanceRepositoryName = repositoryPackage.getInstanceRepositoryName();
        String hostedInstanceRepositoryName = getHostedInstanceRepositoryName(instanceRepositoryName);

        switch(artifactType) {
            case MAVEN:
            case NPM:
            case GENERIC:
                // 删除代理仓库，理论上需要同时删除分组仓库中该代理仓库的关联，但是Nexus3当删除代理仓库时，会自动删除关联关系，因此此处不手动删除分组仓库的关联关系
                /*ApiResponseXO<Void> deleteRepositoryResponse = repositoryManagementService.deleteRepositoryOfAnyFormat(proxyInstanceRepoName);
                if (deleteRepositoryResponse.isSuccess()) {
                    break;
                }
                throw new BizException(deleteRepositoryResponse.getMessage());*/
                DeleteRepositoryFolderRequest deleteRepositoryFolderRequest = new DeleteRepositoryFolderRequest();
                String folderName = repositoryPackage.getNameId();
                if (StrUtil.isNotBlank(repositoryPackage.getGroupId())) {
                    folderName = repositoryPackage.getGroupId().replace(".", "/") + Constant.DEFAULT_URL_PATH_SEPARATOR + repositoryPackage.getNameId();
                }
                deleteRepositoryFolderRequest.setData(Arrays.asList(
                        folderName,
                        hostedInstanceRepositoryName
                        ));
                DeleteRepositoryFolderResponse deleteRepositoryFolderResponse = repositoryFolderService.deleteFolder(deleteRepositoryFolderRequest);
                Optional.ofNullable(deleteRepositoryFolderResponse).flatMap(value -> Optional.ofNullable(value.getResult())).ifPresent(result -> {
                    if (Boolean.FALSE.equals(result.getSuccess())) {
                        throw new BizException("delete nexus folder failed");
                    }
                });
                break;
            case DOCKER:
                String repositoryName = repositoryPackage.getPackageName();
                ApiResponseXO<Void> deleteProjectRepositoryRe =  repositoryService.deleteProjectRepository(instanceRepositoryName, repositoryName);
                if (deleteProjectRepositoryRe.isSuccess()) {
                    break;
                }
                throw new BizException(deleteProjectRepositoryRe.getMessage());
            default:
                throw new BizException("not support this type" + artifactType);
        }
    }
}
